﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using AMS.Common;
using AMS.Configuration;

namespace AMS.UI.Forms
{
    /// <summary>
    /// 一个操作导出设置的辅助类
    /// </summary>
    internal class ExportSettings
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="transCode">索引设置节点的值，不允许为空值</param>
        public ExportSettings(string transCode)
        {
            TransCode = transCode;
            opxml = new XmlOperation();
            if (File.Exists(SettingFilePath))
            {
                opxml.Load(SettingFilePath);
                HasSettings = opxml.IsNodeExists(string.Format(ItemXPath, transCode));
                if (HasSettings)
                {
                    string caption = opxml.GetAttribteValue(string.Format(ItemXPath, transCode), "Captions");
                    if (!string.IsNullOrEmpty(caption))
                    {
                        ExtractCaptions(caption);
                    }
                }
            }
            else
            {
                opxml.LoadXml("<ap></ap>");
            }
        }

        #region Fields & Properties
        /// <summary>
        /// 查询当前设置节点的XPATH
        /// </summary>
        private const string ItemXPath = "/ap/item[@TransCode=\"{0}\"]";
        private XmlOperation opxml;
        /// <summary>
        /// 获取设置文件路径
        /// </summary>
        private string SettingFilePath
        {
            get { return AmsEnvironment.GetSystemPath(SystemPath.Etc) + "ExportSettings.xml"; }
        }

        private List<string> captions = new List<string>();

        /// <summary>
        /// 设置节点的值
        /// </summary>
        public string[] Captions
        {
            get { return HasSettings ? captions.ToArray() : null; }
        }

        /// <summary>
        /// 索引设置节点的值
        /// </summary>
        public string TransCode { get; private set; }
        /// <summary>
        /// 获取一个值，表示是否有当前的设置节点
        /// </summary>
        public bool HasSettings { get; private set; }
        #endregion

        #region Public Methods
        /// <summary>
        /// 测试是否包含指定的字段
        /// </summary>
        /// <param name="caption"></param>
        /// <returns></returns>
        public bool ContainsCaption(string caption)
        {
            if (!HasSettings)
            {
                return false;
            }

            return captions.Contains(caption);
        }

        /// <summary>
        /// 保存已设置了列
        /// </summary>
        /// <param name="captions"></param>
        public void SaveCaptions(string[] captions)
        {
            this.captions.Clear();
            this.captions.AddRange(captions);
            SaveCaptions(string.Join(", ", captions));
        }
        #endregion

        private void ExtractCaptions(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            string[] tokens = value.Split(',');
            foreach (string token in tokens)
            {
                captions.Add(token.Trim());
            }
        }

        private void SaveCaptions(string captions)
        {
            if (!HasSettings)
            {
                opxml.AddOrUpdateNode("/ap", "item", "TransCode", TransCode, null);
            }

            opxml.SetNodeAttribute(string.Format(ItemXPath, TransCode), "Captions", captions);
            opxml.SaveToFile(SettingFilePath);
            HasSettings = true;
        }

    }
}
